package com.ruoyi.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
import java.util.Random;
import java.util.stream.Collectors;

@Slf4j
public class PropertiesDailyGenerator {
    private static final String PROPERTIES_FILE = "daily_random.properties";
    private static final String DATE_KEY_1 = "last_date_bwf";
    private static final String DATE_KEY_2 = "last_date_sort";
    private static final String DATE_KEY_3 = "last_date_stage";

    private static final String NUMBER_VAL_BWF = "daily_number_bwf";
    private static final String NUMBER_VAL_SORT = "daily_number_sort";
    private static final String NUMBER_VAL_STAGE = "daily_number_stage";
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public static void main(String[] args) {
        try {

            String dailyStage = getDailyStage();
            String todayNumber = getDailyNumber();
            String dailySort =  getDailySort();
            //log.info("今天的8位随机数字: " + todayNumber);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("生成随机数字时出错: " + e.getMessage());
            log.error("生成随机数字时出错: " + e.getMessage());
        }
    }

    /**
     * 8位数生成，每天一次
     * @return
     * @throws IOException
     */
    public static String getDailyNumber() throws IOException {

        LocalDate today = LocalDate.now();
        String todayStr = today.format(DATE_FORMATTER);

        Properties props = loadProperties();
        String lastDate = props.getProperty(DATE_KEY_1);
        String lastNumber = props.getProperty(NUMBER_VAL_BWF);

        // 如果今天已经生成过，返回已有的数字
        if (todayStr.equals(lastDate) && lastNumber != null && lastNumber.length() == 8) {
            log.info("返回今天已生成的8位随机数字:"+lastNumber);
            return lastNumber;
        }

        // 生成新的8位随机数字
        String newNumber = generate10DigitNumberV4();

        // 更新Properties并保存
        props.setProperty(DATE_KEY_1, todayStr);
        props.setProperty(NUMBER_VAL_BWF, newNumber);
        saveProperties(props);

        log.info("生成新的8位随机数字:" + newNumber);
        return newNumber;
    }

    public static String generate10DigitNumberV4() {
        Random random = new Random();
        String DIGITS = "023456789"; // 不包含1
        // 第一位从2-9中选（不包含0和1）
        String firstDigit = String.valueOf(random.nextInt(8) + 2);

        // 生成剩余9位
        String remainingDigits = random.ints(7, 0, DIGITS.length())
                .mapToObj(i -> String.valueOf(DIGITS.charAt(i)))
                .collect(Collectors.joining());

        return firstDigit + remainingDigits;
    }

    /**
     * 购买次数
     * @return
     * @throws IOException
     */
    public static String getDailySort() throws IOException {

        LocalDate today = LocalDate.now();
        String todayStr = today.format(DATE_FORMATTER);

        Properties props = loadProperties();
        String lastDate = props.getProperty(DATE_KEY_2);
        String lastNumber = props.getProperty(NUMBER_VAL_SORT);

        // 如果今天已经生成过，返回已有的数字
        if (todayStr.equals(lastDate) && lastNumber != null) {
            log.info("返回今天已生成的2位随机数字:"+lastNumber);

            String addNumber = (Integer.valueOf(lastNumber) + 1) + "";

            // 更新Properties并保存
            props.setProperty(DATE_KEY_2, todayStr);
            props.setProperty(NUMBER_VAL_SORT, addNumber);
            saveProperties(props);

            return addNumber;
        } else {
            // 生成新的2位随机数字
            String newSort = generateRandomSort();

            // 更新Properties并保存
            props.setProperty(DATE_KEY_2, todayStr);
            props.setProperty(NUMBER_VAL_SORT, newSort);
            saveProperties(props);

            log.info("生成新的2位随机数字:" + newSort);
            return newSort;
        }
    }

    private static String generateRandomSort() {
        Random random = new Random();

        // 生成20-50之间的随机数（包含20和50）
        int randomNumber = random.nextInt(31) + 10;
        randomNumber += 1;
        return String.valueOf(randomNumber);
    }

    private static String generateRandomStage(String lastNumber) {
        int newNumber = 1;
        if (lastNumber != null){
            newNumber = Integer.valueOf(lastNumber) + 1;
        }
        return String.valueOf(newNumber);
    }

    /**
     * 开奖期数
     * @return
     * @throws IOException
     */
    public static String getDailyStage() throws IOException {

        LocalDate today = LocalDate.now();
        String todayStr = today.format(DATE_FORMATTER);

        Properties props = loadProperties();
        String lastDate = props.getProperty(DATE_KEY_3);
        String lastNumber = props.getProperty(NUMBER_VAL_STAGE);

        // 如果今天已经生成过，返回已有的数字
        if (todayStr.equals(lastDate) && lastNumber != null) {
            log.info("返回今天已生成的的期数:"+lastNumber);
            return lastNumber;
        } else {
            // 生成新的2位随机数字
            String newStage = generateRandomStage(lastNumber);

            // 更新Properties并保存
            props.setProperty(DATE_KEY_3, todayStr);
            props.setProperty(NUMBER_VAL_STAGE, newStage);
            saveProperties(props);

            log.info("生成新的期数:" + newStage);
            return newStage;
        }
    }

    public static void generateUpadteStage(String date, String stage) {
        try {

            Properties props = loadProperties();
            props.setProperty(DATE_KEY_3, date);
            props.setProperty(NUMBER_VAL_STAGE, stage);
            saveProperties(props);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    static Properties loadProperties() throws IOException {
        Properties props = new Properties();
        File file = new File(PROPERTIES_FILE);
        if (file.exists()) {
            try (FileInputStream fis = new FileInputStream(file)) {
                props.load(fis);
            }
        }
        return props;
    }

    private static void saveProperties(Properties props) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(PROPERTIES_FILE)) {
            props.store(fos, "Daily Random Number Generator");
        }
    }




}
